package cn.fl.continfo.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.continfo.service.IPrjContCfgDtlService;
import cn.fl.continfo.service.IPrjContCfgService;
import cn.fl.continfo.vo.PrjContCfgDtlVO;
import cn.fl.continfo.vo.PrjContCfgVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.condition.PrjContCfgDtlQueryCondition;
import cn.fl.preloan.condition.PrjContCfgQueryCondition;
import cn.fl.preloan.dto.PrjContCfgDTO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cont/prj")
public class PrjContCfgController {

    private static final Logger logger = LoggerFactory.getLogger(PrjContCfgController.class);

    @Autowired
    private IPrjContCfgService prjContCfgService;

    @Autowired
    private IPrjContCfgDtlService prjContCfgDtlService;

    /**
     * 列表查询界面
     */
    @RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "跳转到客户资产信息列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView view = new ModelAndView("continfo/prj/prjContCfgList");
        return view;
    }

    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET })
    public ModelAndView toAddPage() {
        ModelAndView view = new ModelAndView("continfo/prj/prjContCfgAdd");
        BaseLogUtils.info(logger, "toCarAddPage", "跳转到新增修改车辆资产页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET })
    public ModelAndView toModifyPage(@RequestParam("id") Long id) {

        PrjContCfgVO prjContCfgVO = this.prjContCfgService.selectById(id);
        ModelAndView view = new ModelAndView("continfo/prj/prjContCfgAdd");
        view.addObject("prjContCfgVO", prjContCfgVO);
        BaseLogUtils.info(logger, "toModifyPage", "跳转到新增修改车辆资产页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到详情页面
     */
    @RequestMapping(value = "/toReview.do", method = { RequestMethod.GET})
    public ModelAndView toReview(@RequestParam("id") Long id) {
        PrjContCfgVO prjContCfgVO = this.prjContCfgService.selectById(id);

        PrjContCfgDtlQueryCondition qc = new PrjContCfgDtlQueryCondition();
        qc.setCfgCd(prjContCfgVO.getCfgCd());
        List<PrjContCfgDtlVO> list = this.prjContCfgDtlService.searchList(qc);
        List<PrjContCfgDtlVO> wsList = new ArrayList<PrjContCfgDtlVO>();
        List<PrjContCfgDtlVO> zfList = new ArrayList<PrjContCfgDtlVO>();
        if(CollectionUtils.isNotEmpty(list)) {
        	for(PrjContCfgDtlVO vo : list) {
        		if("WS".equals(vo.getContTyp())) {
        			wsList.add(vo);
        		}else {
        			zfList.add(vo);
        		}
        	}
        }
        ModelAndView view = new ModelAndView("continfo/prj/prjContCfgReview");
        view.addObject("prjContCfgVO", prjContCfgVO);
        view.addObject("wsList", wsList);
        view.addObject("zfList", zfList);
        BaseLogUtils.info(logger, "toReview", "跳转到方案详情页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }



    /**
     * 打开合同选择页面
     */
    @RequestMapping(value = "/toContSelectPage.do", method = { RequestMethod.GET})
    public ModelAndView toContSelectPage() {
        ModelAndView view = new ModelAndView("continfo/prj/contSelectPage");
        BaseLogUtils.info(logger, "toCarDetailPage", "跳转到合同选择页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }
    
    /**
     * 到合同列表详情页面
     */
    @RequestMapping(value = "/toDtlListPage.do", method = { RequestMethod.GET})
    public ModelAndView toDtlListPage(@RequestParam("cfgCd") String cfgCd) {
        ModelAndView view = new ModelAndView("continfo/prj/prjContCfgDtlPage");
        view.addObject("cfgCd",cfgCd);
        BaseLogUtils.info(logger, "toCarDetailPage", "跳转到合同详情页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }
    
    /**
     * 到合同详情配置页面
     */
    @RequestMapping(value = "/toContKvPage.do", method = { RequestMethod.GET})
    public ModelAndView toContKvPage(@RequestParam("rule") String rule) {

        ModelAndView view = new ModelAndView("continfo/prj/"+rule+"Dialog");
        BaseLogUtils.info(logger, "toCarDetailPage", "跳转到合同详情配置页面", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 初始化合同方案列表
     */
    @RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String cfgNm = request.getParameter("cfgNm");
        String orgCd = request.getParameter("orgCd");
        String prjTypCd = request.getParameter("prjTypCd");
        String cfgCd = request.getParameter("cfgCd");
        String insCode = request.getParameter("insCode");

        PrjContCfgQueryCondition qc = new PrjContCfgQueryCondition();
        if(StringUtils.isNotBlank(cfgCd)){
            qc.setCfgCd(cfgCd);
        }
        if(StringUtils.isNotBlank(cfgNm)){
            qc.setCfgNm(cfgNm);
        }
        if(StringUtils.isNotBlank(orgCd)){
            qc.setOrgCd(orgCd);
        }
        if(StringUtils.isNotBlank(prjTypCd)){
            qc.setPrjTypCd(prjTypCd);
        }
        if(StringUtils.isNotBlank(insCode)){
            qc.setInsCode(insCode);
        }
        //page.setEveryPage(Integer.MAX_VALUE);//最大数据量
        Pager pager = this.prjContCfgService.searchListPage(Integer.valueOf(pageSize),Integer.valueOf(currentPage),qc);
        ListPageVO listPage = new ListPageVO(pager.getResultList(), pager.getTotalCount());
        BaseLogUtils.info(logger, "initList", "查询合同列表结果:"+ JSONObject.toJSONString(listPage), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }
    
    
    /**
     * 初始化合同方案列表
     */
    @RequestMapping(value = "/initDtlList.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object initDtlList(HttpServletRequest request) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String cfgCd = request.getParameter("cfgCd");

        PrjContCfgDtlQueryCondition qc = new PrjContCfgDtlQueryCondition();
        qc.setCfgCd(cfgCd);

        Pager pager = this.prjContCfgDtlService.searchListPage(Integer.valueOf(pageSize),Integer.valueOf(currentPage),qc);
        ListPageVO listPage = new ListPageVO(pager.getResultList(), pager.getTotalCount());
        BaseLogUtils.info(logger, "initList", "查询合同方案详情列表结果:"+ JSONObject.toJSONString(listPage), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }

    /**
     * 删除车辆资产数据
     */
    @RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
    @ResponseBody
    public DataResultVO removeById(@RequestParam("id") Long id) throws BaseException {
        DataResultVO result = null;
        try {
            boolean flag = this.prjContCfgService.removeById(id);
            BaseLogUtils.info(logger, "removeById", "删除合同方案结果,id="+id+",flag="+flag, CurrentThreadContext.getCurrentUserName());
            if(flag){
                result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
            }else {
                result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_DELETE_FAIL);
            }
        }catch (BaseException ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
            BaseLogUtils.info(logger, "removeById", "删除合同方案!ID:" + id + "-ERROR:"+ ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch (Exception ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removeById", "删除合同方案,系统错误!ID:" + id + "-ERROR:"+ ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }

        return result;
    }


    /**
     * 新增合同方案信息
     */
    @RequestMapping(value = "/add.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object add(PrjContCfgVO entity) throws BaseException {

        DataResultVO result = null;
        try {
        	
            Long id = this.prjContCfgService.add(entity);
            BaseLogUtils.info(logger, "add", "新增合同方案结果,ID:"+id+".DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            if(null != id){
                result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC,id);
            }else {
                result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_ADD_FAIL);
            }
        }catch (BaseException ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
            BaseLogUtils.info(logger, "add", "新增合同方案失败！DATA:" + JSONObject.toJSONString(entity) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch (Exception ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "add", "新增合同方案失败,系统错误！DATA:" + JSONObject.toJSONString(entity) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 合同方案绑定合同
     */
    @RequestMapping(value = "/bindCont.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object bindCont(@RequestBody ArrayList<PrjContCfgDtlVO> list) throws BaseException {

        DataResultVO result = null;
        try {
            boolean flag = this.prjContCfgDtlService.addBatch(list);
            BaseLogUtils.info(logger, "add", "新增合同方案明细结果:"+flag+".DATA:" + JSONObject.toJSONString(list), CurrentThreadContext.getCurrentUserName());
            if(flag){
                result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
            }else {
                result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_ADD_FAIL);
            }
        }catch (BaseException ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
            BaseLogUtils.info(logger, "add", "新增合同方案明细失败！DATA:" + JSONObject.toJSONString(list) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch (Exception ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "add", "新增合同方案明细失败,系统错误！DATA:" + JSONObject.toJSONString(list) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 合同方案删除绑定合同
     */
    @RequestMapping(value = "/delCont.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object delCont(@RequestParam("id") Long id) throws BaseException {

        DataResultVO result = null;
        try {
            List<Long> ids = new ArrayList<>();
            ids.add(id);
            boolean flag = this.prjContCfgDtlService.removeBatchById(ids);
            BaseLogUtils.info(logger, "add", "删除合同方案明细结果:"+flag+".DATA:" + JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            if(flag){
                result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
            }else {
                result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_DELETE_FAIL);
            }
        }catch (BaseException ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
            BaseLogUtils.info(logger, "add", "删除合同方案明细失败！DATA:" + JSONObject.toJSONString(id) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch (Exception ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "add", "删除合同方案明细,系统错误！DATA:" + JSONObject.toJSONString(id) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 修改合同方案
     */
    @RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object modify(PrjContCfgVO entity) throws BaseException {

        DataResultVO result = null;
        if(null == entity.getId()){
            result =  new DataResultVO(BaseConstant.IS_YESNO_NO,SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "modify", "修改合同方案信息主键不存在！.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            return result;
        }
        try {
            boolean flag = this.prjContCfgService.modify(entity);
            BaseLogUtils.info(logger, "modify", "修改合同方案结果,DATA:" + JSONObject.toJSONString(entity)+",flag:"+flag, CurrentThreadContext.getCurrentUserName());
            result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);

        }catch (BaseException ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
            BaseLogUtils.info(logger, "modify", "修改合同方案失败！DATA:" + JSONObject.toJSONString(entity) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch (Exception ex) {
            result = new DataResultVO(BaseConstant.IS_YESNO_NO, SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "modify", "修改合同方案失败,系统错误！DATA:" + JSONObject.toJSONString(entity) + " ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;

    }


}
